/*
 * collect_data.c
 *
 *  Created on: 2024年1月5日
 *      Author: 86176
 */
#include <string.h>
#include "collect_data.h"
#include "fsl_debug_console.h"


extern cJSON *mqtt_json_object;

void init_collect_data(collect_data * data)
{

	memcpy(data->weld_class,"NULL",WELD_CLASS_SIZE);
	memcpy(data->weld_way,"NULL",WELD_WAY_SIZE);
	memcpy(data->weld_polarity,"NULL",WELD_POLARITY_SIZE);
	memcpy(data->weld_angle,"NULL",WELD_ANGLE_SIZE);
	memcpy(data->weld_direction,"NULL",WELD_DIRECTION_SIZE);
	memcpy(data->weld_party,"NULL",WELD_PARTY_SIZE);
//
//
	memcpy(data->weld_device_id,"NULL",WELD_DEVICE_ID_SIZE);
	memcpy(data->weld_id,"NULL",WELD_ID_SIZE);
	memcpy(data->weld_person_id,"NULL",WELD_PERSON_ID_SIZE);
	memcpy(data->weld_layer,"NULL",WELD_LAYER_SIZE);
	memcpy(data->weld_layer_temperature,"NULL",WELD_LAYER_TEMP_SIZE);
//
//
	data->env_temperature = 0;
	data->env_humidness = 0;
	data->env_wind_speed = 0;

	data->weld_voltage = 0;
	data->weld_current = 0;
	data->weld_wire_speed = 0;
	data->weld_speed = 0;
	data->weld_heat_energy = 0;
	data->protection_gas_speed = 0;

	memcpy(data->weld_collect_time,"NULL",20);
	}


void update_collect_data(collect_data * data,collect_event_num event,char * userdata,uint32_t usernum)
{

	switch(event)
	{
		case 0:
			return;
		case 1:
		{
//			memset(data->weld_class,0,strlen(data->weld_class));
			strncpy(data->weld_class,userdata,sizeof(data->weld_class)-1);//更安全的字符串拷贝
			data->weld_class[sizeof(data->weld_class)-1] = '\0';//将字符串结束标识到最后一位
			return;
		}
		case 2:
		{
//			memset(data->weld_way,0,strlen(data->weld_way));
			strncpy(data->weld_way,userdata,sizeof(data->weld_way)-1);
			data->weld_way[sizeof(data->weld_way)-1] = '\0';
			return;
		}
		case 3:
		{
//			memset(data->weld_polarity,0,strlen(data->weld_polarity));
			strncpy(data->weld_polarity,userdata,sizeof(data->weld_polarity)-1);
			data->weld_polarity[sizeof(data->weld_polarity)-1] = '\0';
			return;
		}
		case 4:
		{
//			memset(data->weld_angle,0,strlen(data->weld_angle));
			strncpy(data->weld_angle,userdata,sizeof(data->weld_angle)-1);
			data->weld_angle[sizeof(data->weld_angle)-1] = '\0';
			return;
		}
		case 5:
		{
//			memset(data->weld_direction,0,strlen(data->weld_direction));
			strncpy(data->weld_direction,userdata,sizeof(data->weld_direction)-1);
			data->weld_direction[sizeof(data->weld_direction)-1] = '\0';
			return;
		}
		case 6:
		{
//			memset(data->weld_party,0,strlen(data->weld_party));
			strncpy(data->weld_party,userdata,sizeof(data->weld_party)-1);
			data->weld_party[sizeof(data->weld_party)-1] = '\0';
			return;
		}
		case 7:
		{
//			memset(data->weld_device_id,0,strlen(data->weld_device_id));
			strncpy(data->weld_device_id,userdata,sizeof(data->weld_device_id)-1);
			data->weld_device_id[sizeof(data->weld_device_id)-1] = '\0';
			return;
		}
		case 8:
		{
//			memset(data->weld_id,0,strlen(data->weld_id));
			strncpy(data->weld_id,userdata,sizeof(data->weld_id)-1);
			data->weld_id[sizeof(data->weld_id)-1] = '\0';
			return;
		}
		case 9:
		{
//			memset(data->weld_person_id,0,strlen(data->weld_person_id));
			strncpy(data->weld_person_id,userdata,sizeof(data->weld_person_id)-1);
			data->weld_person_id[sizeof(data->weld_person_id)-1] = '\0';
			return;
		}
		case 10:
		{
//			memset(data->weld_layer,0,strlen(data->weld_layer));
			strncpy(data->weld_layer,userdata,WELD_LAYER_SIZE+1);
			data->weld_layer[sizeof(data->weld_layer)-1] = '\0';
			return;
		}
		case 11:
		{
//			memset(data->weld_layer_temperature,0,strlen(data->weld_layer_temperature));
			strncpy(data->weld_layer_temperature,userdata,sizeof(data->weld_layer_temperature)-1);
			data->weld_layer_temperature[sizeof(data->weld_layer_temperature)-1] = '\0';
			return;
		}
		case 12:
		{
			data->env_temperature = usernum;
			return;
		}
		case 13:
		{
			data->env_humidness = usernum;
			return;
		}
		case 14:
		{
			data->env_wind_speed = usernum;
			return;
		}
		case 15:
		{
			data->weld_voltage = usernum;
			return;
		}
		case 16:
		{
			data->weld_current = usernum;
			return;
		}
		case 17:
		{
			data->weld_wire_speed = usernum;
			return;
		}
		case 18:
		{
			data->weld_speed = usernum;
			return;
		}
		case 19:
		{
			data->weld_heat_energy = usernum;
			return;
		}
		case 20:
		{
			data->protection_gas_speed = usernum;
			return;
		}
		case 21:
		{
//			memset(data->weld_collect_time,0,strlen(data->weld_collect_time));
			memcpy(data->weld_collect_time,userdata,strlen(userdata));
			return;
		}
		default:
		{
			return;
		}
	}
}
/*
	data

	weld_id | weld_device_id | weld_person_id | weld_layer |

	weld_voltage | weld_current | weld_wire_speed | weld_speed |

	weld_layer_temperature | weld_heat_energy | protection_gas_speed |

	env_temperature| env_humidness | env_wind_speed |

	weld_party | weld_direction| weld_class | weld_way | weld_polarity | weld_angle | weld_direction
	weld_collect_time |
*/
void insect_file_data(collect_data * data,char * buffer)
{
	char reading_buffer[200];
	sprintf(reading_buffer,"%s,%s,%s,%s,%d,%d,%d,%d,%s,%d,%d,%d,%d,%d,%s,%s,%s,%s,%s,%s,%s,%s\n\0",\
							data->weld_id,data->weld_device_id,data->weld_person_id,data->weld_layer,\
							data->weld_voltage,data->weld_current,data->weld_wire_speed,data->weld_speed, \
							data->weld_layer_temperature,data->weld_heat_energy,data->protection_gas_speed,\
							data->env_temperature,data->env_humidness,data->env_wind_speed,\
							data->weld_party,data->weld_direction,data->weld_class,data->weld_way,data->weld_polarity,data->weld_angle,data->weld_direction,\
							data->weld_collect_time);
//	PRINTF("reading_buffer:%s\r\n",reading_buffer);
	memcpy(buffer,reading_buffer,strlen(reading_buffer));
}
char* insect_mqtt_data_json(collect_data * data)
{
	cJSON* mqtt_json_object = cJSON_CreateObject();
	cJSON_AddItemToObject(mqtt_json_object, "weld_id", cJSON_CreateString(data->weld_id));
	cJSON_AddItemToObject(mqtt_json_object, "weld_device_id", cJSON_CreateString(data->weld_device_id));
	cJSON_AddItemToObject(mqtt_json_object, "weld_person_id", cJSON_CreateString(data->weld_person_id));
	cJSON_AddItemToObject(mqtt_json_object, "weld_layer", cJSON_CreateString(data->weld_person_id));
	cJSON_AddItemToObject(mqtt_json_object, "weld_voltage", cJSON_CreateNumber(data->weld_voltage));
	cJSON_AddItemToObject(mqtt_json_object, "weld_current", cJSON_CreateNumber(data->weld_current));
	cJSON_AddItemToObject(mqtt_json_object, "weld_wire_speed", cJSON_CreateNumber(data->weld_wire_speed));
	cJSON_AddItemToObject(mqtt_json_object, "weld_speed", cJSON_CreateNumber(data->weld_speed));
	cJSON_AddItemToObject(mqtt_json_object, "weld_layer_temperature", cJSON_CreateString(data->weld_layer_temperature));
	cJSON_AddItemToObject(mqtt_json_object, "weld_heat_energy", cJSON_CreateNumber(data->weld_heat_energy));
	cJSON_AddItemToObject(mqtt_json_object, "protection_gas_speed", cJSON_CreateNumber(data->protection_gas_speed));
	cJSON_AddItemToObject(mqtt_json_object, "env_temperature", cJSON_CreateNumber(data->env_temperature));
	cJSON_AddItemToObject(mqtt_json_object, "env_humidness", cJSON_CreateNumber(data->env_humidness));
	cJSON_AddItemToObject(mqtt_json_object, "env_wind_speed", cJSON_CreateNumber(data->env_wind_speed));
	cJSON_AddItemToObject(mqtt_json_object, "weld_party", cJSON_CreateString(data->weld_party));
	cJSON_AddItemToObject(mqtt_json_object, "weld_direction", cJSON_CreateString(data->weld_direction));
	cJSON_AddItemToObject(mqtt_json_object, "weld_class", cJSON_CreateString(data->weld_class));
	cJSON_AddItemToObject(mqtt_json_object, "weld_way", cJSON_CreateString(data->weld_way));
	cJSON_AddItemToObject(mqtt_json_object, "weld_polarity", cJSON_CreateString(data->weld_polarity));
	cJSON_AddItemToObject(mqtt_json_object, "weld_angle", cJSON_CreateString(data->weld_angle));
	cJSON_AddItemToObject(mqtt_json_object, "weld_direction", cJSON_CreateString(data->weld_direction));
	cJSON_AddItemToObject(mqtt_json_object, "weld_collect_time", cJSON_CreateString(data->weld_collect_time));
	char *buffer = cJSON_Print(mqtt_json_object);
	cJSON_Delete(mqtt_json_object);
	return buffer;
}



